package com.nbjtjc.safe.api.edu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduRequirementInvestigationMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduRequirementInvestigation;
import com.nbjtjc.safe.model.edu.EduRequirementRespondent;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.service.edu.EduRequirementInvestigationService;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("/eduReqInv")
public class EduRequirementInvestigationController {
	@Autowired
	private EduRequirementInvestigationService eduRequirementInvestigationService;
	@Autowired
	private EduRequirementInvestigationMapper eduRequirementInvestigationMapper;
	@Autowired
	private SysNoticeService sysNoticeService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    

	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
//	  @SysLog("添加或更新需求调查")
	    public Result add(@RequestBody EduRequirementInvestigation eduRequirementInvestigation){
	        Result result=new Result();
	        try {
	        	int ifAdd=0;
	        	if(ObjectUtil.isNull(eduRequirementInvestigation)){
	        		ifAdd=1;
	        	}
	        	
	            result=eduRequirementInvestigationService.add(eduRequirementInvestigation);
	            
	            //通知参与人员
	            if(ifAdd==1&&result.isSuccess()==true
	            		&&ObjectUtil.isNotNull((EduRequirementInvestigation) result.getData())
	            		&&CollectionUtil.isNotEmpty(((EduRequirementInvestigation) result.getData()).getEduRequirementRespondents())){
	            	SysNotice sysNotice = new SysNotice();
	 	            sysNotice.setContent("请参与需求调查。");
	 	            sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
	 	            sysNotice.setWriteUserId(MyShiroUtils.getSessionUser().getUserId());
	 	            sysNotice.setDeptId(MyShiroUtils.getDeptId());
	 	            sysNotice.setCreateTime(new Date());
	 	            sysNotice.setIdType(6);
	 	            sysNotice.setRelateId(eduRequirementInvestigation.getId());
	 	            sysNotice.setNoticeType(0);
	 	            sysNotice.setTitle("需求调查通知");
	 	            List<Integer> userIds=new ArrayList();
	 	            for(EduRequirementRespondent eduRequirementRespondent:((EduRequirementInvestigation) result.getData()).getEduRequirementRespondents()){
	 	            	if(ObjectUtil.isNotNull(eduRequirementRespondent)&&ObjectUtil.isNotNull(eduRequirementRespondent.getUserId())){
		 	            	userIds.add(eduRequirementRespondent.getUserId());
	 	            	}
	 	            }
	 	            boolean f = sysNoticeService.addNoticeList(sysNotice, userIds);
	 	            if(!f){
	 	            	result.setFalse("通知发送失败");
	 	            }
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  @RequestMapping(value = "/save",method = {RequestMethod.POST})
//	  @SysLog("保存未发布的需求调查")
	    public Result save(@RequestBody EduRequirementInvestigation eduRequirementInvestigation){
	        Result result=new Result();
	        try {
	            result=eduRequirementInvestigationService.save(eduRequirementInvestigation);
	    		//通知选中的人员
	            if(ObjectUtil.isNotNull(eduRequirementInvestigation)&&CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
	            	SysNotice sysNotice = new SysNotice();
	 	            sysNotice.setContent("正在发布需求调查，需相关管理员录入参与人员");
	 	            sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
	 	            sysNotice.setWriteUserId(MyShiroUtils.getSessionUser().getUserId());
	 	            sysNotice.setDeptId(MyShiroUtils.getDeptId());
	 	            sysNotice.setCreateTime(new Date());
	 	            sysNotice.setIdType(6);
	 	            sysNotice.setRelateId(eduRequirementInvestigation.getId());
	 	            sysNotice.setNoticeType(0);
	 	            sysNotice.setTitle("需求调查通知");
	 	            List<Integer> userIds=new ArrayList();
	 	            for(EduRequirementRespondent eduRequirementRespondent:eduRequirementInvestigation.getEduRequirementRespondents()){
	 	            	if(ObjectUtil.isNotNull(eduRequirementRespondent)&&ObjectUtil.isNotNull(eduRequirementRespondent.getUserId())){
		 	            	userIds.add(eduRequirementRespondent.getUserId());
	 	            	}
	 	            }
	 	            boolean f = sysNoticeService.addNoticeList(sysNotice, userIds);
	 	            if(!f){
	 	            	result.setFalse("通知发送失败");
	 	            }
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	 
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除需求调查")
	    public Result delete(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result=new Result();
	        try {
	            result=eduRequirementInvestigationService.delete(eduRequirementInvestigation.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/restart", method = {RequestMethod.POST})
	  public Result restart(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
	            result = eduRequirementInvestigationService.restart(eduRequirementInvestigation);
	    		//通知未填人员
	            if(ObjectUtil.isNotNull(result.getData())){
	            	SysNotice sysNotice = new SysNotice();
	 	            sysNotice.setContent("需求调查已延长，以前未完成，需要补填");
	 	            sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
	 	            sysNotice.setWriteUserId(MyShiroUtils.getSessionUser().getUserId());
	 	            sysNotice.setDeptId(MyShiroUtils.getDeptId());
	 	            sysNotice.setCreateTime(new Date());
	 	            sysNotice.setIdType(6);
	 	            sysNotice.setRelateId(eduRequirementInvestigation.getId());
	 	            sysNotice.setNoticeType(0);
	 	            sysNotice.setTitle("补填需求调查");
	 	            boolean f = sysNoticeService.addNoticeList(sysNotice,(List<Integer>) result.getData());
	 	            if(!f){
	 	            	result.setFalse("通知发送失败");
	 	            }
	            }
	           
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findSimple", method = {RequestMethod.POST})
	  public Result findSimple(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
	        	
	    		if(ObjectUtil.isNull(eduRequirementInvestigation.getCompanyId())||eduRequirementInvestigation.getCompanyId()<=0){
	    			eduRequirementInvestigation.setCompanyId(MyShiroUtils.getCompanyId());
	    		}
	            result = eduRequirementInvestigationService.find(eduRequirementInvestigation);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  //普通员工 的查询，只能查看本公司的需求调查
	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
	    		if(ObjectUtil.isNull(eduRequirementInvestigation.getCompanyId())||eduRequirementInvestigation.getCompanyId()<=0){
	    			eduRequirementInvestigation.setCompanyId(MyShiroUtils.getCompanyId());
	    		}
	            result = eduRequirementInvestigationService.findDetail(eduRequirementInvestigation);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
//	  //管理人员的查询，非本公司的需求调查也可查看
//	  @RequestMapping(value = "/managerFind", method = {RequestMethod.POST})
//	  public Result managerFind(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
//	        Result result = new Result();
//	        try {
//
//				result = eduRequirementInvestigationService.manegerFindDetail(eduRequirementInvestigation);
//	        } catch (Exception e) {
//	            e.printStackTrace();
//	            result.setFalse(e.getMessage());
//	        }
//	        return result;
//	    }
//	  
	  //管理人员的查询，非本公司的需求调查也可查看
	  @RequestMapping(value = "/managerFind", method = {RequestMethod.POST})
	  public Result managerFind(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
        		PageHelper.startPage(eduRequirementInvestigation.getPageCurrent(), eduRequirementInvestigation.getPageSize());

	        	List<Integer> sysDeptIds = new ArrayList();
	        	SysDept sysDept = new SysDept();
	        	sysDept = sysDeptMapper.selectByPrimaryKey(MyShiroUtils.getCompanyId());
	        	sysDeptIds = sysDeptService.getAllParentCompanyId(sysDept, sysDeptIds);
				List<EduRequirementInvestigation> eduRequirementInvestigations = eduRequirementInvestigationMapper.managerFindDetail(eduRequirementInvestigation,sysDeptIds);
				result.setData(new PageInfo<>(eduRequirementInvestigations));
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  //查询下级单位详细数据
	  @RequestMapping(value = "/findSubcompany", method = {RequestMethod.POST})
	  public Result findSubcompany(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
	            result = eduRequirementInvestigationService.findSubcompany(eduRequirementInvestigation);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  //计算已参与需求调查人员总数
	  @RequestMapping(value = "/totalRespondent", method = {RequestMethod.POST})
	  public Result totalRespondent(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {
	            result = eduRequirementInvestigationService.totalRespondent(eduRequirementInvestigation.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/show", method = {RequestMethod.POST})
	  public Result show(@RequestBody EduRequirementInvestigation eduRequirementInvestigation) {
	        Result result = new Result();
	        try {	            
	            result = eduRequirementInvestigationService.show(eduRequirementInvestigation);  
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
}
